/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package other;

import com.jme3.asset.AssetManager;
import com.jme3.cinematic.MotionPath;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.Camera;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class Wizard
{

    private static AssetManager assetManager;
    private static Camera camera;
    private static CameraNode cameraNode;
    private static ElementList elementList;
    /*----------------------------------------------------------------------------------------------------------------------------------------------*/

    public static Object loadFile(String absolutePath)
    {
        File f = new File(absolutePath);
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try
        {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException ex)
        {
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(null, "The program will now crash :D",
                    "!", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
        try
        {
            ois = new ObjectInputStream(fis);
        } catch (IOException ex)
        {
        }
        try
        {
            Object o = ois.readObject();
            try
            {
                fis.close();
                ois.close();
                return o;
            } catch (IOException ex)
            {
            }
            return ois.readObject();
        } catch (IOException ex)
        {
        } catch (ClassNotFoundException ex)
        {
        }
        return null;
    }

    public static void saveFile(String filePath, Object object)
    {
        File f = new File(filePath);
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try
        {
            fos = new FileOutputStream(f);
        } catch (FileNotFoundException ex)
        {
        }
        try
        {
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
            fos.close();
            oos.close();
        } catch (IOException ex)
        {
        }
    }

    public static HashMap readMat(File f)
    {
        String zeile;
        BufferedReader in;
        HashMap</*Value, Type*/String, String> hashMap = new HashMap<String, String>();
        try
        {
            in = new BufferedReader(new FileReader(f));
            zeile = null;
            boolean kE = false;
            while ((zeile = in.readLine()) != null)
            {
                // zeile nicht innerhalb der MaterialParameters?
                if (!zeile.contains("{") && !zeile.contains("//") && !kE && !(zeile.trim().length() == 0))
                {
                    if (zeile.contains("}"))
                    {
                        kE = true;
                    } else
                    {
                        zeile = zeile.trim();
                        StringTokenizer st = new StringTokenizer(zeile, " ");
                        String fToken = st.nextToken();
                        String sToken = st.nextToken();
                        hashMap.put(sToken, fToken);
                    }
                }
            }
        } catch (FileNotFoundException ex)
        {
            Logger.getLogger(Wizard.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            Logger.getLogger(Wizard.class.getName()).log(Level.SEVERE, null, ex);
        }

        return hashMap;
    }

    /**
     * @param colorRGBA
     * @return a Swing-Color
     */
    public static Color makeColor(ColorRGBA colorRGBA)
    {
        return new Color(colorRGBA.r, colorRGBA.g, colorRGBA.b, 1);
    }

    /**
     * @param color
     * @return a Monkey-Color
     */
    public static ColorRGBA makeColorRGBA(Color color)
    {
        return new ColorRGBA(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f);
    }

    public static ImageIcon resizeImage(Image originalImage, int w, int h, boolean keepRelation)
    {
        int width = w;
        int height = h;
        if (keepRelation)
        {
            if (w < h)
            {
                height = width;
            } else
            {
                width = height;
            }
        }
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = resizedImage.createGraphics();
        g.setColor(new Color(65, 61, 61));
        g.fillRect(0, 0, width, height);
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        ImageIcon icon = new ImageIcon(resizedImage);
        return icon;
    }

    public static Image resizeImageI(Image originalImage, int w, int h)
    {
        BufferedImage resizedImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, w, h, null);
        g.dispose();
        return resizedImage;
    }

    public static void setWireframe(Spatial spatial, boolean wireframeEnabled)
    {
        if (!spatial.getName().equals("Light Scattering Node")
                && !spatial.getName().equals("Light Node"))
        {
            if (spatial instanceof Node)
            {
                Node node = (Node) spatial;
                for (int i = 0; i < node.getQuantity(); i++)
                {
                    Spatial child = node.getChild(i);
                    setWireframe(child, wireframeEnabled);
                }
            } else if (spatial instanceof Geometry)
            {
                Geometry geo = (Geometry) spatial;
                geo.getMaterial().getAdditionalRenderState().setWireframe(wireframeEnabled);
            }
        }
    }

    public static void insertAllGeometrys(Node node, Vector<Geometry> geometrys)
    {
        for (Spatial child : node.getChildren())
        {
            if (child instanceof Node)
            {
                insertAllGeometrys((Node) child, geometrys);
            } else if (child instanceof Geometry)
            {
                geometrys.add((Geometry) child);
            }
        }
    }

    /**
     * Set the AssetManager that is supposed to be used
     */
    public static void setAssetManager(AssetManager _assetManager)
    {
        assetManager = _assetManager;
    }

    /**
     * Set the Camera that is supposed to be used (for MotionEvents for
     * instance)
     */
    public static void setCamera(Camera _camera)
    {
        camera = _camera;
    }

    public static AssetManager getAssetManager()
    {
        return assetManager;
    }

    public static Camera getCamera()
    {
        return camera;
    }

    public static CameraNode getCameraNode()
    {
        return cameraNode;
    }

    public static void setCameraNode(CameraNode cameraNode)
    {
        Wizard.cameraNode = cameraNode;
    }

    public static float calculateDistance(MotionPath motionPath, int waypointsDone)
    {
        float distance = 0;
        if (waypointsDone == motionPath.getNbWayPoints())
        {
            return distance;
        } else
        {
            for (int i = waypointsDone; i < motionPath.getNbWayPoints() - 1; i++)
            {
                distance += motionPath.getWayPoint(i).distance(motionPath.getWayPoint(i + 1));
            }
            return distance;
        }
    }

    public static ElementList getElementList()
    {
        return elementList;
    }

    public static void setElementList(ElementList elementList)
    {
        Wizard.elementList = elementList;
    }
}